package com.glsc.ngateway.opmanage.controller.outsys;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.*;
import com.glsc.ngateway.opmanage.dto.OutSystemQueryDto;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemAuthTemplateService;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemService;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemTemplateRelService;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemTemplateService;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 外部系统权限模板主表
 */
@RestController
@RequestMapping("/api/outSystem/template")
@Slf4j
public class OutSystemTemplateController {

    @Resource
    private OutSystemTemplateService templateService;

    @Resource
    private OutSystemTemplateRelService relService;

    @Resource
    private OutSystemAuthTemplateService authTemplateService;

    @Resource
    private OutSystemService outSystemService;


    /**
     * 分页查询
     *
     * @param outSystemQueryDto
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<OutSystemTemplate>> findClientPage(@RequestBody OutSystemQueryDto outSystemQueryDto) {
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(outSystemQueryDto.getName())) {
            param.put("LIKE_name", outSystemQueryDto.getName());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getPostName())) {
            param.put("LIKE_postName", outSystemQueryDto.getPostName());
        }

        Page<OutSystemTemplate> page = templateService.page(PageUtil.initPageNo(outSystemQueryDto.getPageNo()), PageUtil.initPageSize(outSystemQueryDto.getPageSize()), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据客户id查询客户信息
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<OutSystemTemplate> findById(@ApiParam(value = "id") @RequestParam(required = true) Integer id, @ApiParam(value = "sysId") @RequestParam(required = false) Integer sysId) {
        OutSystemTemplate template = templateService.findById(id);
        template.setSystemTemplateRelList(Lists.newArrayList());
        List<OutSystemTemplateRel> list = relService.findByTemplateId(template.getId(), sysId);
        if (CollectionUtils.isNotEmpty(list)) {
            for (OutSystemTemplateRel rel : list) {
                OutSystemInfo system = outSystemService.findSystemById(rel.getSysId());
                rel.setSystemName(system.getSysName());
            }
            template.setSystemTemplateRelList(list);
        }
        return PlatformResponse.successData(template);
    }

    /**
     * 保存
     *
     * @param outSystemAuthInfo
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<OutSystemTemplate> save(@RequestBody OutSystemTemplate outSystemAuthInfo) {
        return PlatformResponse.successData(templateService.save(outSystemAuthInfo));
    }

    /**
     * 删除接口
     *
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody OutSystemTemplate outSystemAuthInfo) {
        templateService.deleteById(outSystemAuthInfo.getId());
        return PlatformResponse.success();
    }

    /**
     * 查询所有的模板信息和与模板关联的系统信息
     *
     * @return
     */
    @GetMapping("/findAllTemplateAndSystem")
    public PlatformResponse<List<OutSystemTemplate>> findAllTemplateAndSystem() {
        List<OutSystemTemplate> templateList = templateService.findAll();
        if (CollectionUtils.isEmpty(templateList)) {
            return PlatformResponse.successData(Lists.newArrayList());
        }

        List<OutSystemInfo> outSystemInfos = outSystemService.listAllUseFullSystem();
        Map<Integer, OutSystemInfo> systemMap = outSystemInfos.stream().collect(Collectors.toMap(OutSystemInfo::getId, e -> e, (i, j) -> j));

        for (OutSystemTemplate template : templateList) {
            List<OutSystemTemplateRel> list = relService.findByTemplateId(template.getId(), null);
            if (CollectionUtils.isNotEmpty(list)) {
                for (OutSystemTemplateRel rel : list) {
                    OutSystemInfo systemInfo = systemMap.get(rel.getSysId());
                    if (Objects.nonNull(systemInfo)) {
                        rel.setSystemName(systemInfo.getSysName());
                    }
                }
            }
            template.setSystemTemplateRelList(list);
        }
        return PlatformResponse.successData(templateList);
    }


}
